// index.js
// 获取应用实例
const app = getApp()
// eslint-disable-next-line no-undef
const plugin = requirePlugin('zsPlugin')
import {
    getProjectList,
    getMyQueryUserInfoApi
} from '../../api/app.js'
import {
    goSalaryCard
} from '../../utils/salary-card.js'
import {
    goAppList,
    goToContract,
    dateFormat,
    goApp
} from '../../utils/util'
import user from '../../utils/user.js'

Page({
    data: {
        openId: user.openid,
        top: app.globalData.menuTop,
        menu: [{
            icon: '/assets/img/space/record.png',
            text: '工作记录',
            num: 0,
            path: 'applet::/pages/record/record',
            istitle: true
        },
        {
            icon: '/assets/img/space/sign-up.png',
            text: '我的报名',
            path: 'applet::/pages/my-sign-up/my-sign-up',
            istitle: true
        },
        {
            icon: '/assets/img/space/advance.png',
            text: '我的预支',
            path: 'applet::/packageAdvance/pages/advance-index/advance-index',
            istitle: true,
            prepositionunit: '￥'
        },
        {
            icon: '/assets/img/space/payroll.png',
            text: '工资单',
            path: 'applet::/pages/payroll-list/payroll-list',
            istitle: true,
            postpositionunit: '条'
        },
        {
            icon: '/assets/img/space/icon-card.png',
            text: '我的微工卡',
            path: 'applet::/pages/card/card',
            hide: true
        },
        // {
        //     icon: '/assets/img/space/real.png',
        //     text: '实名认证',
        //     path: 'applet::/pages/personal-info/personal-info',
        //     hide: true
        // },
        {
            icon: '/assets/img/space/icon-message.png',
            text: '打开消息通知',
            path: 'https://mp.weixin.qq.com/s/9SgP2C0QTwOu2AVZoJMp1Q'
        },
        {
            icon: '/assets/img/space/icon-assitant.png',
            text: '客服助手',
            time: '9:00-18:00',
            path: 'applet::/pages/service/service',
            hide: false
        },
        {
            icon: '/assets/img/space/icon-setting.png',
            text: '账号设置',
            path: 'applet::/pages/logout/user-logout/user-logout'
        },
        {
            icon: '/assets/img/space/about.png',
            text: '关于',
            path: 'applet::/pages/handset-info/handset-info',
            hide: true
        },
        {
            icon: '/assets/img/space/opinion.png',
            text: '意见反馈',
            path: 'applet::/pages/opinion/opinion',
            hide: true
        },
        ],
        userName: '',
        salaryStatus: false,
        hasCardPhoto: false,
        isDEV: app.globalData.env !== 'release',
        colorList: ['#B4EBFF', '#D4BEFF', '#FFB3E3', '#FFCDB3', '#bcd9ff', '#ffbec2', '#ffdfb9'],
        colorIndex: '',
        colorName: '',
        userHeadUrl: '',
        userId: '',
        refreshKey: 1
    },
    onReady() {
        // name:this.properties.content.substring(this.properties.content.length-2,this.properties.content.length)
        this.setData({
            colorIndex: this.data.colorList[Math.floor(Math.random() * 7)],
            colorName: user.userName.substring(user.userName.length - 2, user.userName.length)
        }, () => {
            console.log(this.data.colorIndex, 'color')
        })

    },
    // async onShow() {
    //     this.getTabBar().setData({
    //         selected: 2
    //     })
    //     await user.waitLogin()
    //     this.setData({
    //         userName: user.userName,
    //         userHeadUrl: user.avatarUrl,
    //         userId: user.userId
    //     })
    //     if (user.userId) {
    //         this.setData({
    //             hasCardPhoto: user.hasCardPhoto,
    //             salaryStatus: user.isOpenSalaryCard
    //         })
    //         if (!this.data.menu[0] || !this.data.menu[0].num) {
    //             this.getRecordNum()
    //         }
    //     }
    // },
    async onShow() {
        this.getTabBar().setData({
            selected: 3
        })
        await user.waitLogin()
        this.setData({
            openId: user.openid,
            userName: user.userName,
            userHeadUrl: user.avatarUrl,
            userId: user.userId
        })
        if (user.userId) {
            this.setData({
                hasCardPhoto: user.hasCardPhoto,
                salaryStatus: user.isOpenSalaryCard
            })
            if (!this.data.menu[0] || !this.data.menu[0].num) {
                // this.getRecordNum()
                this.getMyQueryUserInfo()
            }
        }
        const real = this.data.menu.find(i => i.path === 'applet::/pages/personal-info/personal-info')
        if (real) {
            real.hide = !user.userId
        }
        const microCard = this.data.menu.find(i => i.path === 'applet::/pages/card/card')
        console.log('hjdfsgdhyughyd', !this.data.salaryStatus, microCard)
        if (microCard) {
            microCard.hide = !this.data.salaryStatus
        }
        this.setData({
            menu: this.data.menu
        })
    },
    async getMyQueryUserInfo() {
        const result = await getMyQueryUserInfoApi()
        console.log(result, 'ghdiushgiu')
        this.setData({
            ['menu[0].num']: result.workCount,
            ['menu[1].num']: result.jobCount,
            ['menu[2].num']: result.advanceAmount,
            ['menu[3].num']: result.stubCount
        })
    },
    async getRecordNum() {
        const result = await getProjectList({
            id: user.userId
        })
        this.setData({
            ['menu[0].num']: result.length
        })
    },
    goSalaryCard() {
        goSalaryCard()
    },
    async goPath(event) {
        // if (!user.userId) {
        //     await app.confirm('您还未注册，是否立即前往注册？')
        //     user.goRegister()
        //     return 0
        // }
        console.log(event, 'eventeventeventeventevent')
        if (event.currentTarget.dataset.path == 'https://mp.weixin.qq.com/s/9SgP2C0QTwOu2AVZoJMp1Q') {
            goAppList([{
                url: 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxa68344cdd579b406&redirect_uri=https://front.51zidan.com/mini-auth/bullet.html?' + encodeURIComponent('openid=' + user.openid) + '&response_type=code&scope=snsapi_base#wechat_redirect',
                time: 3000
            }, {
                url: 'https://mp.weixin.qq.com/s/9SgP2C0QTwOu2AVZoJMp1Q',
                type: 'redirectTo' // 重定向跳转到关注页面，用户少返回一个界面
            }])
        } else if (event.currentTarget.dataset.path === 'applet::/pages/card/card') {
            goSalaryCard()
        } else {
            goApp(event.currentTarget.dataset.path)
        }
    },
    onBankCard() {
        console.log('跳转到银行卡')
        wx.navigateTo({
            url: '/pages/bank-card/bank-card',
        })
    },
    goLogin() {
        user.goRegister()
    },
    goTeamList() {
        wx.navigateTo({
            url: '/packageA/pages/team-list/team-list?id=' + user.companyId
        })
    },
    addTeamList() {
        wx.navigateTo({
            url: '/packageA/pages/master-invitation-qrcode/master-invitation-qrcode'
        })
    },
    onRealName() {
        goApp('applet::/pages/personal-info/personal-info')
    },
    async onPullDownRefresh() {
        this.setData({
            refreshKey: 0
        })
        await user.reLogin()
        setTimeout(() => {
            this.setData({
                refreshKey: Math.ceil(Math.random() * 1000000)
            })
        }, 50)
        this.getRecordNum()
        wx.stopPullDownRefresh()
    },
    bannerCallBack(e) {
        let item = e.detail.item
        if (item.jumpType == 2) {
            let url = encodeURIComponent(item.h5Url)
            wx.navigateTo({ // 需创建一个webview页面，供h5页面跳转，页面名称可自定义，但需与此处跳转匹配。
                url: '/pages/zsWebView/zsWebView?url=' + url,
            })
        } else if (item.jumpType == 3) {
            wx.openEmbeddedMiniProgram({
                appId: item.appId,
                path: item.appPath
            })
        } else if (item.jumpType == 6) {
            let obj = plugin.getUrlInfo(item)
            wx.navigateToMiniProgram({
                appId: obj.appId,
                path: obj.pages,
                extraData: obj.miniProData
            })
        }
    },

})